<%@page import="java.util.List"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="coba.service.ProvinciaLocalServiceUtil"%>
<%@page import="coba.service.InstitucionLocalServiceUtil"%>
<%@page import="coba.service.ContactoLocalServiceUtil"%>
<%@page import="coba.service.RecorridoLocalServiceUtil"%>
<%@page import="coba.service.GuiaLocalServiceUtil"%>
<%@page import="coba.model.Provincia"%>
<%@page import="coba.model.Institucion"%>
<%@page import="coba.model.Contacto"%>
<%@page import="coba.model.Recorrido"%>
<%@page import="coba.model.Guia"%>
<%@page import="javax.portlet.PortletURL"%>
<%@page import="javax.portlet.ActionRequest"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>

<portlet:defineObjects />

<%
	PortletURL urlList = renderResponse.createRenderURL();
	urlList.setParameter("jspPage","/views/guia/list.jsp");

	PortletURL urlGuiaForm = renderResponse.createActionURL();
	urlGuiaForm.setParameter(ActionRequest.ACTION_NAME, "guiaForm");
	urlGuiaForm.setParameter("redirectTo", urlList.toString());
	
	Long guiaId = renderRequest.getParameter("id") != null &&
			!renderRequest.getParameter("id").trim().equals("") ?
			Long.valueOf(renderRequest.getParameter("id")) :
			null;

	Guia guia = guiaId != null ?
			GuiaLocalServiceUtil.getGuia(guiaId) :
			null;
%>

<script>
	function validarRangoFechas() {
		var fechaInicio = Date.parse($('#fechaInicio').val())
		var fechaFin = Date.parse($('#fechaFin').val())

		if (fechaInicio > fechaFin)
			document.getElementById('fechaFin').setCustomValidity('La fecha de fin debe ser mayor o igual a la fecha de inicio.')
		else
			document.getElementById('fechaFin').setCustomValidity('')
	}
</script>

<div>
	<div class="taglib-header">
		<span class="header-back-to">
			<a class="icon-circle-arrow-left previous-level" href="<%=urlList%>" title="Atr&aacute;s">
				<span class="helper-hidden-accessible"> Atr&aacute;s </span>
			</a>
		</span>
		<h3 class="header-title"><span> Gu&iacute;a </span></h3>
	</div>

	<div>
		<form action="<%=urlGuiaForm %>" method="post">
			<input type="hidden" name="<portlet:namespace />id" value="<%=guia != null ? guia.getGuiaId() : ""%>" />

			<div class="form-group">
				<label for="nombre">Nombre</label>
				<input required maxlength="50" type="text" class="form-control campo" name="<portlet:namespace />nombre" id="nombre" value="<%=guia != null ? guia.getNombre() : ""%>">
			</div>

			<div class="form-group">
				<label for="apellido">Apellido</label>
				<input required maxlength="50" type="text" class="form-control campo" name="<portlet:namespace />apellido" id="apellido" value="<%=guia != null ? guia.getApellido() : ""%>">
			</div>

			<div class="form-group">
				<label for="email">E-mail</label>
				<input required maxlength="50" type="email" class="form-control campo" name="<portlet:namespace />email" id="email" value="<%=guia != null ? guia.getEmail() : ""%>">
			</div>

			<div class="form-group">
				<label for="telefono">Tel&eacute;fono</label>
				<input required maxlength="50" type="tel" class="form-control campo" name="<portlet:namespace />telefono" id="telefono" value="<%=guia != null ? guia.getTelefono() : ""%>">
			</div>

			<div class="form-group">
				<label for="dias">D&iacute;as</label>
					<div>
						<input style="margin:0 !important" type="checkbox" name="<portlet:namespace />dia_1" id="dia_lunes" <%=guia != null && guia.getDias().contains("1") ? "checked" : "" %> />
						<label style="display:inline" for="dia_lunes">Lunes</label>
					</div>
					<div>
						<input style="margin:0 !important" type="checkbox" name="<portlet:namespace />dia_2" id="dia_martes" <%=guia != null && guia.getDias().contains("2") ? "checked" : "" %> />
						<label style="display:inline" for="dia_lunes">Martes</label>
					</div>
					<div>
						<input style="margin:0 !important" type="checkbox" name="<portlet:namespace />dia_3" id="dia_miercoles" <%=guia != null && guia.getDias().contains("3") ? "checked" : "" %> />
						<label style="display:inline" for="dia_lunes">Mi&eacute;rcoles</label>
					</div>
					<div>
						<input style="margin:0 !important" type="checkbox" name="<portlet:namespace />dia_4" id="dia_jueves" <%=guia != null && guia.getDias().contains("4") ? "checked" : "" %> />
						<label style="display:inline" for="dia_lunes">Jueves</label>
					</div>
					<div>
						<input style="margin:0 !important" type="checkbox" name="<portlet:namespace />dia_5" id="dia_viernes" <%=guia != null && guia.getDias().contains("5") ? "checked" : "" %> />
						<label style="display:inline" for="dia_lunes">Viernes</label>
					</div>
					<div>
						<input style="margin:0 !important" type="checkbox" name="<portlet:namespace />dia_6" id="dia_sabado" <%=guia != null && guia.getDias().contains("6") ? "checked" : "" %> />
						<label style="display:inline" for="dia_lunes">S&aacute;bado</label>
					</div>
			</div>

			<div class="form-group">
				<label for="fechaInicio">Fecha Inicio</label>
				<input required type="date" class="form-control campo" name="<portlet:namespace />fechaInicio" id="fechaInicio" oninput="validarRangoFechas()" value="<%=guia != null ? new SimpleDateFormat("yyyy-MM-dd").format(guia.getFechaInicio()) : ""%>">
			</div>

			<div class="form-group">
				<label for="fechaFin">Fecha Fin</label>
				<input type="date" class="form-control campo" name="<portlet:namespace />fechaFin" id="fechaFin" oninput="validarRangoFechas()" value="<%=guia != null && guia.getFechaFin() != null ? new SimpleDateFormat("yyyy-MM-dd").format(guia.getFechaFin()) : ""%>">
			</div>
			
			<div>
				<button type="submit" class="btn btn-primary">Aceptar</button>
			</div>
		</form>
	</div>	
</div>
